/*
 *
 * Copyright (C) 2008 Jose Javier García Zornoza.
 *
 * Este es un programa libre; puedes distribuirlo y/o modificarlo bajo
 * los términos de la versión 2 (o cualquier versión posterior) de la Licencia
 * Pública General de la Fundación de Software Libre.
 * Este programa se distribuye con la esperanza  de poder ser útil, pero sin
 * NINGUNA GARANTIA; tampoco la garantía implícita de COMERCIALIZACION o DEDICACIÓN
 * A UN PROPOSITO PARTICULAR. Ver la licencia GNU GPL para más detalles.
 * Deberías haber recibido una copia de la licencia GNU GPL con este programa;
 * en caso contrario puedes obtener una copia visitando el sitio web www.fsf.org
 * o escribiendo a la Fundación de Software Libre, Inc., 59 Temple
 * Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */
package org.gruposp2p.dnie.ui.dialogs;

import dniegp2pclient.DNIeGP2PClientApp;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.jdesktop.application.ResourceMap;

public class PasswordDialog extends javax.swing.JDialog {

    private static Logger logger = LoggerFactory.getLogger(PasswordDialog.class);

    private ResourceMap resourceMap;
    private String password;
    private JFrame parentFrame;

    /** Creates new form PasswordDialog */
    public PasswordDialog(JFrame parentFrame, boolean modal) {
        super(parentFrame, modal);
        this.parentFrame = parentFrame;
        this.setLocationRelativeTo(parentFrame);
        this.setTitle("Introduzca la contrase\u00F1a");
        initComponents();
        password1Field.addKeyListener(new KeyListener(){
            public void keyPressed(KeyEvent e){
                if (e.getKeyCode() == KeyEvent.VK_ENTER)
                acceptButton.doClick();
            }
            public void keyTyped(KeyEvent ke) {}
            public void keyReleased(KeyEvent ke) {}
        });
        password2Field.addKeyListener(new KeyListener(){
            public void keyPressed(KeyEvent e){
                if (e.getKeyCode() == KeyEvent.VK_ENTER)
                acceptButton.doClick();
            }
            public void keyTyped(KeyEvent ke) {}
            public void keyReleased(KeyEvent ke) {}
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        password1Field = new javax.swing.JPasswordField();
        labelConfirmar = new javax.swing.JLabel();
        password2Field = new javax.swing.JPasswordField();
        cancelButton = new javax.swing.JButton();
        acceptButton = new javax.swing.JButton();
        labelIcono = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        password1Field.setText(org.jdesktop.application.Application.getInstance().getContext().getResourceMap(PasswordDialog.class).getString("password1Field.text")); // NOI18N
        password1Field.setName("password1Field"); // NOI18N

        labelConfirmar.setText(org.jdesktop.application.Application.getInstance().getContext().getResourceMap(PasswordDialog.class).getString("labelConfirmar.text")); // NOI18N
        labelConfirmar.setName("labelConfirmar"); // NOI18N

        password2Field.setText(org.jdesktop.application.Application.getInstance().getContext().getResourceMap(PasswordDialog.class).getString("password2Field.text")); // NOI18N
        password2Field.setName("password2Field"); // NOI18N

        cancelButton.setIcon(org.jdesktop.application.Application.getInstance().getContext().getResourceMap(PasswordDialog.class).getIcon("cancelButton.icon")); // NOI18N
        cancelButton.setText(org.jdesktop.application.Application.getInstance().getContext().getResourceMap(PasswordDialog.class).getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarHandler(evt);
            }
        });

        acceptButton.setIcon(org.jdesktop.application.Application.getInstance().getContext().getResourceMap(PasswordDialog.class).getIcon("acceptButton.icon")); // NOI18N
        acceptButton.setText(org.jdesktop.application.Application.getInstance().getContext().getResourceMap(PasswordDialog.class).getString("acceptButton.text")); // NOI18N
        acceptButton.setName("acceptButton"); // NOI18N
        acceptButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                acceptButtonActionPerformed(evt);
            }
        });

        labelIcono.setIcon(org.jdesktop.application.Application.getInstance().getContext().getResourceMap(PasswordDialog.class).getIcon("labelIcono.icon")); // NOI18N
        labelIcono.setText(org.jdesktop.application.Application.getInstance().getContext().getResourceMap(PasswordDialog.class).getString("labelIcono.text")); // NOI18N
        labelIcono.setName("labelIcono"); // NOI18N

        jLabel1.setText(org.jdesktop.application.Application.getInstance().getContext().getResourceMap(PasswordDialog.class).getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(acceptButton, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelIcono, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(labelConfirmar, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
                            .addComponent(password2Field)
                            .addComponent(password1Field)))
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(password1Field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelConfirmar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(password2Field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(labelIcono, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(acceptButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelarHandler(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarHandler
        this.dispose();
    }//GEN-LAST:event_cancelarHandler

    private void acceptButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acceptButtonActionPerformed
        String password1 = new String (password1Field.getPassword());
        String password2 = new String (password2Field.getPassword());
        if (password1.equals(password2)) {
            password = password1;
            this.dispose();
        } else {
            PasswordErrorDialog passwordErrorDialog =
                                new PasswordErrorDialog (parentFrame, true);
            passwordErrorDialog.setVisible(true);
            password1Field.setText("");
            password2Field.setText("");
        }
    }//GEN-LAST:event_acceptButtonActionPerformed

    public String getPassword() {
        return password;
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton acceptButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel labelConfirmar;
    private javax.swing.JLabel labelIcono;
    private javax.swing.JPasswordField password1Field;
    private javax.swing.JPasswordField password2Field;
    // End of variables declaration//GEN-END:variables

}
